﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Events.Chapter2
{
    //public delegate void WorkPerformedHandler(int hours,WorkType workType);

    public class Worker
    {
        public event EventHandler<WorkPerformentEventArgs> WorkPerfomed;
        public event EventHandler WorkCompleted;

        public void DoWork(int hours, WorkType workType)
        {
            for (int i = 0; i < 50; i++)
            {
                System.Threading.Thread.Sleep(100);
                OnWorkPerformed(hours + i, workType);
            }

            OnWorkCompleted();
        }

        protected virtual void OnWorkPerformed(int p, WorkType workType)
        {
            var workDel = WorkPerfomed as EventHandler<WorkPerformentEventArgs>;
            if (workDel != null)
                workDel(this, new WorkPerformentEventArgs(p, workType));
        }

        protected virtual void OnWorkCompleted()
        {
            var workDel = WorkCompleted as EventHandler;
            if (workDel != null)
                workDel(this, EventArgs.Empty);
        }
    }
}
